package com.cskaoyan.gateway.controller.shopping;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cskaoyan.gateway.form.shopping.CheckAllCartItemForm;
import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.shopping.ICartService;
import com.mall.shopping.dto.CheckAllItemRequest;
import com.mall.shopping.dto.DeleteCheckedItemRequest;
import com.mall.shopping.dto.DeleteCheckedItemResposne;
import com.mall.user.annotation.Anoymous;
import com.mall.user.intercepter.TokenIntercepter;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @Classname ItemsController
 * @Description TODO
 * @author王茁
 * @Date 2022/1/20 17:19
 */
@Slf4j
@RestController
@RequestMapping("shopping")
public class ItemsController {

    @Reference(timeout = 3000, retries = 2, check = false)
    ICartService cartService;

    @DeleteMapping("items/{uid}")
    /**
     * @description: 删除购物车中选中的商品
     * @param:
     * @return:
     * @author wangzhuo
     * @date: 2022-01-20 20:21
     */
    //  public ResponseData deleteCheckedItem(@PathVariable("uid") Long uid, HttpServletRequest request) {
    public ResponseData deleteCheckedItem(HttpServletRequest request) {
        //不从request里面取userId,直接从Token里面取
        String attribute = (String) request.getAttribute(TokenIntercepter.USER_INFO_KEY);
        JSONObject parse = (JSONObject) JSON.parse(attribute);
        if (parse.get("uid") == null) {
            return new ResponseUtil<>().setData("传入参数为空");
        }
        Long uid = Long.parseLong(parse.get("uid").toString());

        DeleteCheckedItemRequest deleteCheckedItemRequest = new DeleteCheckedItemRequest();
        deleteCheckedItemRequest.setUserId(uid);
        //deleteCheckedItemRequest.setUserId(74L);
        cartService.deleteCheckedItem(deleteCheckedItemRequest);
        return new ResponseUtil<>().setData("成功");
    }

    @PutMapping("items")
    /**
     * @description: 全选购物车中的商品
     * @param:
     * @return:
     * @author wangzhuo
     * @date: 2022-01-21 21:10
     */
    public ResponseData checkAllCartItem(@RequestBody CheckAllCartItemForm map, HttpServletRequest request) {
      /*  Long userId = (Long) map.get("userId");
        String checked = (String) map.get("checked");*/
        //Long userId = map.getUserId();
        //不从request里面取userId,直接从Token里面取
        String attribute = (String) request.getAttribute(TokenIntercepter.USER_INFO_KEY);
        JSONObject parse = (JSONObject) JSON.parse(attribute);
        if (parse.get("uid") == null) {
            return new ResponseUtil<>().setData("传入参数为空");
        }
        Long userId = Long.parseLong(parse.get("uid").toString());
        String checked = map.getChecked();
        CheckAllItemRequest checkAllItemRequest = new CheckAllItemRequest();
        checkAllItemRequest.setChecked(checked);
        checkAllItemRequest.setUserId(userId);
        cartService.checkAllCartItem(checkAllItemRequest);
        return new ResponseUtil<>().setData("成功");
    }
}
